package com.ruoyi.system.api.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.Configuration;
import com.ruoyi.system.api.mapper.ConfigurationMapper;
import com.ruoyi.system.api.service.CcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.api.mapper.FseLocationLogMapper;
import com.ruoyi.system.api.domain.FseLocationLog;
import com.ruoyi.system.api.service.IFseLocationLogService;
import com.ruoyi.common.core.text.Convert;
import sun.util.resources.ga.LocaleNames_ga;

/**
 * FSE位置坐标日志Service业务层处理
 * 
 * @author shihx
 * @date 2020-03-04
 */
@Service
public class FseLocationLogServiceImpl implements IFseLocationLogService 
{
    @Autowired
    private FseLocationLogMapper fseLocationLogMapper;

    @Autowired
    private ConfigurationMapper configurationMapper;
    @Autowired
    private CcService ccService;
    /**
     * 查询FSE位置坐标日志
     * 
     * @param id FSE位置坐标日志ID
     * @return FSE位置坐标日志
     */
    @Override
    public FseLocationLog selectFseLocationLogById(Long id)
    {
        return fseLocationLogMapper.selectFseLocationLogById(id);
    }

    /**
     * 查询FSE位置坐标日志列表
     * 
     * @param fseLocationLog FSE位置坐标日志
     * @return FSE位置坐标日志
     */
    @Override
    public List<FseLocationLog> selectFseLocationLogList(FseLocationLog fseLocationLog)
    {
        return fseLocationLogMapper.selectFseLocationLogList(fseLocationLog);
    }

    /**
     * 新增FSE位置坐标日志
     * 
     * @param fseLocationLog FSE位置坐标日志
     * @return 结果
     */
    @Override
    public int insertFseLocationLog(FseLocationLog fseLocationLog)
    {
        fseLocationLog.setCreateTime(DateUtils.getNowDate());
        return fseLocationLogMapper.insertFseLocationLog(fseLocationLog);
    }

    /**
     * 修改FSE位置坐标日志
     * 
     * @param fseLocationLog FSE位置坐标日志
     * @return 结果
     */
    @Override
    public int updateFseLocationLog(FseLocationLog fseLocationLog)
    {
        fseLocationLog.setUpdateTime(DateUtils.getNowDate());
        return fseLocationLogMapper.updateFseLocationLog(fseLocationLog);
    }

    /**
     * 删除FSE位置坐标日志对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFseLocationLogByIds(String ids)
    {
        return fseLocationLogMapper.deleteFseLocationLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除FSE位置坐标日志信息
     * 
     * @param id FSE位置坐标日志ID
     * @return 结果
     */
    @Override
    public int deleteFseLocationLogById(Long id)
    {
        return fseLocationLogMapper.deleteFseLocationLogById(id);
    }

    /**
     * 查询历史轨迹
     * @param locationLog
     * @return
     */
    @Override
    public ResponseWithData<?> getFseLocationLog(FseLocationLog locationLog) {
        List<FseLocationLog> list = fseLocationLogMapper.getFseLocationLog(locationLog);
        return new ResponseWithData<>().success(list).msg("查询成功").code(200);
    }

    /**
     * 查询异常fse
     * @param params
     * @return
     */
    @Override
    public ResponseWithData<?> getErrorFse(Map<String, String> params) {
        String teamleaderId = params.get("teamleaderId");
        if (StringUtils.isNotEmpty(teamleaderId)) {
            List<FseLocationLog> list = fseLocationLogMapper.getErrorFse(params);
            return new ResponseWithData<>().success(list).msg("查询成功").code(200);
        } else {
            return new ResponseWithData<>().success().msg("查询失败").code(201);
        }
    }

    /**
     * 查询Team下fse
     * @param params
     * @return
     */
    @Override
    public ResponseWithData<?> selectTeamFseLocal(Map<String, String> params) {
        List<FseLocationLog> list = fseLocationLogMapper.selectTeamFseLocal(params);
        Configuration configuration=new Configuration();
        configuration.setName("network_state");
        List<Configuration> configurations = configurationMapper.selectConfigurationList(configuration);
        Long network_stateConf=new Long(configurations.get(0).getValue());
        for (FseLocationLog log :list){
            Date receiveTime= log.getReceiveDate();
            if(receiveTime!=null){
                long diff = new Date().getTime() - receiveTime.getTime();
                long nd=1000*24*60*60;
                long nh=1000*60*60;
                long nm=1000*60;
                long min = diff % nd % nh/nm;
                if(min<network_stateConf){
                    //在线
                    log.setOnline("1");
                }else{
                    // 离线
                    log.setOnline("0");
                }
            }else{
                log.setOnline("1");
            }
        }
        return new ResponseWithData<>().success(list).msg("查询成功").code(200);
    }

    /**
     * 历史轨迹fse下拉内容
     * @param params
     * @return
     */
    @Override
    public ResponseWithData<?> selectFseByTlid(Map<String, String> params) {
        List<Map<String, Object>> list = fseLocationLogMapper.selectFseByTlid(params);
        return new ResponseWithData<>().success(list).msg("查询成功").code(200);
    }

    //备件转移fse接口
    @Override
    public ResponseWithData<?> selectTransferFse(Map<String, String> params){

        String fseId = params.get("enginnerId");
           String fseName=params.get("enginnerName");
           if(StringUtils.isNotEmpty(fseName)){
               return ccService.selectTransferWithName(fseName);
           }
           if(StringUtils.isNotEmpty(fseId)){
               return ccService.selectTransferWithOutName(fseId);
           }
        return new ResponseWithData<>().error("查询参数错误").code(201);

    }
}
